﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Skywave_Windows_Forms_Sample
{
    public partial class Demo_FormDesigner : Form
    {
        public Demo_FormDesigner()
        {
            InitializeComponent();
            //
            profilesManager1.InitializeMe(Program._Path_Documents + "FormDesigner");
            //
            advancedPrintDocumentSettings1.InitializeMe(Program._Path_Documents + @"FormDesigner\PrintSettings");
            //
            formDesigner1.AddTextField("CurrentDateTime", Properties.Resources.Demo_FormDesginer_CurrentDateTime, "Cat 1");
            formDesigner1.AddTextField("Counter", Properties.Resources.Demo_FormDesginer_Counter, "Other");
            formDesigner1.InitializeMe();
        }

        private void profilesManager1_ProfileLoad(object sender, Skywave.Windows.Forms.ProfilesManagerProfileEventArgs e)
        {
            formDesigner1.LoadFrom(e.Content);
        }

        private void profilesManager1_ProfileStore(object sender, Skywave.Windows.Forms.ProfilesManagerProfileEventArgs e)
        {
            formDesigner1.SaveTo(e.Content);
        }

        private void buttonPreview_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.ShowDialog(this);
        }

        int printCounter;
        private void advancedPrintDocument1_PrintEntirePage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            advancedPrintDocument1.PrintAreaHeight = formDesigner1.PalleteSize.Height;
            advancedPrintDocument1.PrintAreaWidth = formDesigner1.PalleteSize.Width;
        }

        private void advancedPrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            formDesigner1.PrintOn(e.Graphics, new System.Drawing.Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, formDesigner1.PalleteSize.Height));
            printCounter++;
            e.HasMorePages = printCounter <= 10;
        }

        private void advancedPrintDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            printCounter = 1;
        }

        private void formDesigner1_SetFieldValue(object sender, Skywave.Windows.Forms.RuntimeDesign.FormDesignerSetFieldValueEventArgs e)
        {
            if (e.Field.FieldType == typeof(string))
            {
                if (e.Field.FieldTag is string)
                {
                    switch ((string)e.Field.FieldTag)
                    {
                        case "CurrentDateTime":
                            e.Field.FieldValue = DateTime.Now.ToString();
                            break;
                        case "Counter":
                            e.Field.FieldValue = printCounter.ToString();
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}
